﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Common;
using SdmxMl.Helper;

namespace SdmxMl.UC
{
    public partial class UcBase : UserControl
    {
        protected TreeNode treeNode;
        protected ISdmxHierarchyListItem item = null;
        protected IHierarchyManage fhl = null;
        protected string idNaming = "ID '";
        protected UcMlText ucMlText = null;

        public UcBase()
        {
            InitializeComponent();
            ucMlText = new UcMlText();
        }

        public virtual bool BindFromNode(TreeNode node, bool isFinal)
        {
            treeNode = node;
            item = null;
            fhl = null;
            if (node != null)
            {
                item = node.Tag as ISdmxHierarchyListItem;
                TreeNode fhlNode = TreeHelper.GetHierarchyManage(node);
                if (fhlNode != null)
                    fhl = fhlNode.Tag as IHierarchyManage;
            }
            return Visible;
        }


        public virtual bool UpdateNodeTag(TreeNode node)
        {
            bool err = false;
            if (node != null && treeNode != null)
            {
                err = BindFromControls();
                if (item != null && treeNode.Text != item.ToString())
                    treeNode.Text = item.ToString();
            }

            return err; // true on error
        }

        protected virtual bool BindFromControls()
        {
            bool err = false;

            return err;
        }

        protected void FillParentInfo(TextBox parentIdBox, TextBox parentDescrBox)
        {
            parentIdBox.Text = string.Empty;
            parentDescrBox.Text = string.Empty;
            if (fhl != null && item != null)
            {
                ISdmxHierarchyListItem it = fhl.GetParent(item);
                if (it != null)
                {
                    parentIdBox.Text = GetParentIdText(it);
                    parentDescrBox.Text = it.Name.ToString();
                }
            }
        }
        protected string GetParentIdText(ISdmxHierarchyListItem it)
        {
            SdmxMl.Structure.Category cat = it as SdmxMl.Structure.Category;
            if (cat != null)
                return cat.FullId;
            SdmxMl.Structure.ProcessStepType ps = it as SdmxMl.Structure.ProcessStepType;
            if (ps != null)
                return ps.FullId;
            return it.Id;
        }
        protected bool CheckUniqueId(TextBox idBox)
        {
            return CheckUniqueId(idBox, fhl);
        }

        protected bool CheckUniqueId(TextBox idBox, IHierarchyManage fhl)
        {
            bool err = false;
            if (item != null)
            {
                if (idBox.Text.Trim() == string.Empty)
                {
                    MessageBox.Show(idNaming + idBox.Text + "' cannot be empty.\nReplaced by " + item.Id, "Editor",
                       MessageBoxButtons.OK, MessageBoxIcon.Error);
                    idBox.Text = item.Id;
                    err = true;
                }
                else if (item.Id != idBox.Text)
                {
                    if (fhl.RenameHierarchicalItem(item, idBox.Text))
                    {
                        MessageBox.Show(idNaming + idBox.Text + "' is already defined.\nReplaced by " + item.Id, "Editor",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        idBox.Text = item.Id;
                        err = true;
                    }
                    treeNode.Text = item.ToString();
                }
            }
            return err;
        }

    }
}
